<?
global $VALUTA;
$file = UPLOAD_PATH."report-".date('d.m.Y', $time_end).".xls";
$filename = str_replace(" ", "-", $me['small_fio'])."-".$me['level_str']."-".date('d.m.Y', $time_end).".xls";


require_once APPPATH.'libraries/PHPExcel.php';
$excel = new PHPExcel();
$excel->getProperties()->setCreator("MMMANK")
						->setLastModifiedBy("MMMANK")
						->setTitle("Money report")
;


/******************************/


$styleMinusFill = array(
	'fill' => array(
		'type'	=> PHPExcel_Style_Fill::FILL_SOLID,
		'color' => array(
			'argb'	=> 'FFA2C1E8'
		)
	),
	'alignment' => array(
		'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
	)
);

$stylePlusFill = $styleMinusFill;
$stylePlusFill['fill']['color']['argb'] = 'FFE6B9B8';

$styleBalanceFill = $styleMinusFill;
$styleBalanceFill['fill']['color']['argb'] = 'FFD7E4BD';


$minus_column = 'B';
$minus_column_end = chr(ord($minus_column) + count($itogo) - 1);
$plus_column = chr(ord($minus_column) + count($itogo));
$plus_column_end = chr(ord($plus_column) + count($itogo) - 1);
$balance_column = chr(ord($plus_column) + count($itogo));
$balance_column_end = chr(ord($balance_column) + count($itogo) - 1);
$summ_column = chr(ord($balance_column) + count($itogo));
$summ_column_end = chr(ord($summ_column) + count($itogo) - 1);
$users_column = chr(ord($summ_column) + count($itogo));
$users_column2 = chr(ord($users_column) + 1);
$new_users_column = chr(ord($users_column2) + 1);

$excel->setActiveSheetIndex(0);
$excel->getActiveSheet()->setTitle('Отчёт по деньгам');
$excel->getActiveSheet()->getColumnDimension('A')->setWidth(30);

//  Первая строка - заголовки
$excel->getActiveSheet()
		->setCellValue($minus_column.'2', 'Выплачено')
		->setCellValue($plus_column.'2', 'Поступило')
		->setCellValue($balance_column.'2', 'Баланс')
		->setCellValue($summ_column.'2', 'Всего')
		->setCellValue($users_column.'2', 'Участников')
		->setCellValue($users_column2.'2', 'Участников с операциями')
		->setCellValue($new_users_column.'2', 'Новичков')
		;



//  Вторая строка - валюты
$column = ord($minus_column);
for($i = 0; $i < 4; $i++)
{
	foreach($itogo as $valuta => $arr)
	{
		$excel->getActiveSheet()->setCellValue(chr($column).'3', $VALUTA[$valuta]);
		$column++;
	}
}



//  Третья строка - цифры

$excel->getActiveSheet()->setCellValue('A4', $me['small_fio']." (".$me['level_str'].")");

$column = ord($minus_column);

// выплачено
foreach($itogo as $valuta => $arr)
{
	$c = round(@$arr['minus'], 2);
	$excel->getActiveSheet()->setCellValue(chr($column).'4', $c);
	$column++;
}

// поступило
foreach($itogo as $valuta => $arr)
{
	$c = round(@$arr['plus'], 2);
	$excel->getActiveSheet()->setCellValue(chr($column).'4', $c);
	$column++;
}

// баланс
$i = 0;
foreach($itogo as $valuta => $arr)
{
	$plus_column_valuta = chr(ord($plus_column) + $i);
	$minus_column_valuta = chr(ord($minus_column) + $i);
	$excel->getActiveSheet()->setCellValue(chr($column).'4', '='.$plus_column_valuta."4-".$minus_column_valuta."4");
	$column++;
	$i++;
}

// всего на счетах
foreach($itogo as $valuta => $arr)
{
	$c = round(@$arr['summ'], 2);
	$excel->getActiveSheet()->setCellValue(chr($column).'4', $c);
	$column++;
}


// участников
$excel->getActiveSheet()->setCellValue($users_column.'4', $count_children);

// участников с вкладами
$excel->getActiveSheet()->setCellValue($users_column2.'4', $count_children_oper);

// новичков
$excel->getActiveSheet()->setCellValue($new_users_column.'4', $count_new_users);







$excel->getActiveSheet()->getStyle($minus_column.'2:'.$new_users_column.'4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

$excel->getActiveSheet()->mergeCells($minus_column.'2:'.$minus_column_end."2");
$excel->getActiveSheet()->mergeCells($plus_column.'2:'.$plus_column_end."2");
$excel->getActiveSheet()->mergeCells($balance_column.'2:'.$balance_column_end."2");
$excel->getActiveSheet()->mergeCells($summ_column.'2:'.$summ_column_end."2");

$excel->getActiveSheet()->getStyle($minus_column.'2:'.$minus_column_end."3")->applyFromArray($styleMinusFill);
$excel->getActiveSheet()->getStyle($plus_column.'2:'.$plus_column_end."3")->applyFromArray($stylePlusFill);
$excel->getActiveSheet()->getStyle($balance_column.'2:'.$balance_column_end."3")->applyFromArray($styleBalanceFill);

$excel->getActiveSheet()->getColumnDimension($users_column)->setWidth(13);
$excel->getActiveSheet()->getColumnDimension($users_column2)->setWidth(25);
$excel->getActiveSheet()->getColumnDimension($new_users_column)->setWidth(13);

$excel->getActiveSheet()->getStyle($minus_column.'2:'.$minus_column_end."3")->getFont()->setBold(true);
$excel->getActiveSheet()->getStyle($plus_column.'2:'.$plus_column_end."3")->getFont()->setBold(true);
$excel->getActiveSheet()->getStyle($balance_column.'2:'.$balance_column_end."3")->getFont()->setBold(true);
$excel->getActiveSheet()->getStyle($summ_column.'2:'.$summ_column_end."3")->getFont()->setBold(true);
$excel->getActiveSheet()->getStyle($users_column.'2:'.$users_column."3")->getFont()->setBold(true);
$excel->getActiveSheet()->getStyle($users_column2.'2:'.$users_column2."3")->getFont()->setBold(true);
$excel->getActiveSheet()->getStyle($new_users_column.'2:'.$new_users_column."3")->getFont()->setBold(true);
$excel->getActiveSheet()->getStyle('A4')->getFont()->setBold(true);


/******************************/



$excel->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel5');
$objWriter->save($file);


header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$filename);
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
$output = file_get_contents($file);
@unlink($file);
die($output);



?>